package markgame2d.engine;

import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.List;

import markgame2d.engine.interfaces.Alive;
import markgame2d.engine.interfaces.Collideable;
import markgame2d.engine.interfaces.Updateable;


public class MarkGameListUtils {

	public static void removeDead(List<? extends Alive> list){
		for (int i = 0; i < list.size();) {
			Alive obj = list.get(i);
			if (obj.isAlive()) {
				i++;
			} else {
				list.remove(i);
			}
		}
	}

	public static void testCollision(Collideable c, List<? extends Collideable> list){
		if(c.isAlive()){
			Rectangle bounds = c.getBounds();
			for (Collideable collideable : list) {
				if(collideable != c){
					if(collideable.isAlive()){
						if(bounds.intersects(collideable.getBounds())){
							c.onColide(collideable);
							collideable.onColide(c);
						}
					}
				}
			}
		}
	}

	public static void testCollision(List<? extends Collideable> list){
		for (int i = 0; i < list.size(); i++) {
			Collideable ci = list.get(i);
			if(ci.isAlive()){
				Rectangle bi = ci.getBounds();
				for (int j = i+1; j < list.size(); j++) {
					Collideable cj = list.get(j);
					if(cj.isAlive()){
						Rectangle bj = cj.getBounds();
						if(bi.intersects(bj)){
							ci.onColide(cj);
							cj.onColide(ci);
						}
					}
				}
			}
		}
	}

	public static void testCollision(List<? extends Collideable> list1, List<? extends Collideable> list2){
		for (Collideable collideable : list1) {
			testCollision(collideable, list2);
		}
	}

	public static void update(List<? extends Updateable> list, float time){
		for (Updateable updateable : list) {
			updateable.update(time);
		}
	}

	public static void draw(List<? extends MarkSprite> list, Graphics2D g){
		for (MarkSprite sprite : list) {
			sprite.draw(g);
		}
	}

}
